<template>
  <div class="search">
    <Form ref="form" :model="formItem" :rules="formItemRules" :label-width="100">
      <FormItem label="策略名称" prop="policyName">
        <Input v-model="formItem.policyName" placeholder="请输入内容"></Input>
      </FormItem>
      <FormItem label="策略类型" prop="policyType">
        <Select v-model="formItem.policyType">
          <Option :value="0" label="拒绝-黑名单"></Option>
          <Option :value="1" label="允许-白名单"></Option>
        </Select>
      </FormItem>
      <FormItem label="IP地址/域名" prop="ipAddress">
        <Input
          v-model="formItem.ipAddress"
          type="textarea"
          placeholder="192.168.0.1;192.168.0.2;baidu.com;weixin.com"></Input> 同时支持Ip和域名,多个用分号";"隔开。示例：192.168.0.1;baidu.com;weixin.com
      </FormItem>
    </Form>
  </div>
</template>

<script>
export default {
  name: 'RateLimitInfo',
  data () {
    return {
      isAdd: true,
      formItemRules: {
        policyName: [
          { required: true, message: '策略名称不能为空', trigger: 'blur' }
        ],
        ipAddress: [
          { required: true, message: 'Ip地址不能为空', trigger: 'blur' }
        ]
      },
      formItem: this.getFormItem()
    }
  },
  methods: {
    getFormItem () {
      return {
        policyId: '',
        policyName: '',
        policyType: 0,
        ipAddress: ''
      }
    },
    setData (data) {
      if (data && data.policyId) {
        Object.assign(this.formItem, {
          policyId: data.policyId,
          policyName: data.policyName,
          policyType: data.policyType,
          ipAddress: data.ipAddress
        })
      }
    },
    handleReset () {
      this.$refs['form'].resetFields()
      this.formItem = this.getFormItem()
    },
    handleSubmit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.$http.post(this.$apis.ipLimit.save, this.formItem, this)
              .then(res => {
                resolve(res)
              }).catch(function (err) {
                reject(err)
              })
          } else {
            resolve(false)
          }
        })
      })
    }
  },
  created () {

  },
  mounted () {
  }
}
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
